﻿using System;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using InTheHand.Net.Sockets;
using WinHBM.Core.Bluetooth;
using WinHBM.Core.Net.Http;
using WinHBM.Core.Net.Sockets;
using WinHBM.Core.Net.Sockets.Cmd;
using WinHBM.Core.DB;
using System.Data.SQLite;
using System.Collections.Generic;
using WinHBM.Utils;
using WinHBM.Core.Task;
using System.Runtime.InteropServices;

namespace WinHBM
{
    public partial class MainForm : Form
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(MainForm));

        private FlashSocketServer flashSocketServer = FlashSocketServer.Instance;

        private HttpClient httpClient = HttpClient.Instance;

        private SynchronizationContext context;


        private static MainForm instance;



        #region Disable Flash's Context Menu
        private const int GWL_WNDPROC = -4;
        public delegate IntPtr FlaWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        private IntPtr OldWndProc = IntPtr.Zero;
        private FlaWndProc Wpr = null;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, FlaWndProc wndProc);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        private IntPtr FlashWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
        {
            if (msg == 516)
            {

                if(MessageBox.Show("确认退出吗？","提示",MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.Close();
                }
                
                return (IntPtr)0;
            }
                
            return CallWindowProc(OldWndProc, hWnd, msg, wParam, lParam);
        }
        #endregion




        public static MainForm INSTANCE {
            get
            {
                if(instance == null)
                {
                    instance = new MainForm();
                }
                return instance;
            }
        }

        public MainForm()
        {
            InitializeComponent();
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Wpr = new FlaWndProc(this.FlashWndProc);
            this.OldWndProc = SetWindowLong(this.player.Handle, GWL_WNDPROC, Wpr);

            this.context = SynchronizationContext.Current;
            flashSocketServer.CommandListener += FlashSocketServer_CommandListener;
            
            this.player.LoadMovie(0, Application.StartupPath + @"\res\Main.swf");
            this.player.Play();
        }

        private void FlashSocketServer_CommandListener(Newtonsoft.Json.Linq.JObject request)
        {

            context.Post(PostSafeCallback,request);
        }

        protected void PostSafeCallback(object state)
        {
            Newtonsoft.Json.Linq.JObject request = (Newtonsoft.Json.Linq.JObject)state;
            string cmd = request["cmd"].ToString();
            if (cmd.Equals(SocketCmd.CMD_WINDOW_MINSIZE))
            {
                Logger.Debug("minsize window");
                this.WindowState = FormWindowState.Minimized;
            }
            else if (cmd.Equals(SocketCmd.CMD_WINDOW_EXIT))
            {
                Logger.Debug("exit window");
                this.Close();
            }
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
#if DEBUG
            this.Size = new System.Drawing.Size(852, 479);
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = FormBorderStyle.Sizable;
#endif
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
            {
                MessageBox.Show("禁止操作");
                e.Handled = true;
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (BluetoothClientHolder.Online)
            {
                BluetoothClientHolder.magicBar.Offline();
                BluetoothClientHolder.bluetoothClient.Close();
                BluetoothClientHolder.bluetoothClient = null;
                BluetoothClientHolder.magicBar = null;
                BluetoothClientHolder.Address = "";
                BluetoothClientHolder.Online = false;
            }
            //Application.Exit();
            System.Environment.Exit(0);
        }

        
    }
}
